package de.ubschmidt.capi;

import java.nio.ByteBuffer;

/**
 * This message should be used by applications to indicate compatibility
 * with an incoming call. This command sends an ALERT to the network to
 * prevent the call from expiring (?No user responding?). If an application
 * is able to accept the call immediately, it need not use this message,
 * but can issue a CONNECT_RESP to COMMON-ISDN-API directly.
 *
 * <p>An ALERT_REQ message must always specify a PLCI which has been
 * previously received via a CONNECT_IND. Therefore, the two constructors of
 * {@code AlertReq} also require either a PLCI directly or a
 * {@code ConnectInd}, which in turn contains the PLCI.
 */
public class AlertReq
        extends CapiActiveMessage
        implements PlciProperty {

    /** The PLCI. */
    private int _plci;

    /** */
    private CapiStruct _additionalInfo;

    /**
     * Creates a new ALERT_REQ message for the specified PLCI.
     *
     * @param plci The PLCI.
     */
    public AlertReq(final int plci) {
        super(Command.ALERT_REQ);
        setPlci(plci);
    }

    /**
     * Creates a new ALERT_REQ message for the PLCI which is contained in
     * the specified CONNECT_IND message.
     *
     * @param connectInd The CONNECT_IND message.
     */
    public AlertReq(final ConnectInd connectInd) {
        this(connectInd.getPlci());
    }

    /** {@inheritDoc} */
    public int getPlci() {
        return _plci;
    }

    /** {@inheritDoc} */
    public void setPlci(final int plci) {
        _plci = plci;
    }

    /** {@inheritDoc} */
    protected void serializeTo(final ByteBuffer byteBuffer) {
        byteBuffer.putInt(getPlci());
        byteBuffer.put((byte) 0);  // TODO additional info
    }

    /** {@inheritDoc} */
    @Override
    public String toString() {
        String s = String.format("%s[%s plci=0x%08x addInfo=...]",
                getClass().getSimpleName(),
                super.toString(),
                getPlci());

        return s;
    }

}
